<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/10
 * Time: 9:07
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Appraise as App;
class Appraise extends Common
{
    public function appraise()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_display'] = array('eq',0);
            if (isset($pagedata['nickname']) && $pagedata['nickname'] != '') {
                $where['nickname'] = array('like', '%' . $pagedata['nickname'] . '%');
            }
            if (isset($pagedata['content']) && $pagedata['content'] != '') {
                $where['content'] = array('like', '%' . $pagedata['content'] . '%');
            }
            $app = new App();
            $count = $app->getAppraiseCount($where);
            $data = $app->getAppraiseList($where,$startrow,$pagesize);
            ajax_return(config('code.success_show'),'请求成功',$data,$count);

        } else {
            return view();
        }
    }

    //删除
    public function delAppraise()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "app_id = {$post['id']}";
            $app = new App();
            $res = $app->appraiseDel($where);
            if ($res) {
                ajax_return(0,'删除成功');
            } else {
                ajax_return(1,'删除失败');
            }
        }
    }
   /**
    * [getAppInfo 获取评价详情]
    * @Author   kendy
    * @DateTime 2020-01-08T11:11:46+0800
    * @return   [type]                   [description]
    */
    public function getAppInfo(){

         if (Request::instance()->isAjax()) {
            $post = Request::instance()->post(); 
            $where['order_id'] = $post['order_id'];
            $where['type'] = $post['type'];
            $app = new App();
            $res = $app->getAppInfo($where);
            if(!empty($res)){
                $res['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
                if($res['eval_status']==1) $res['eval_status'] = "好评";
                else $res['eval_status'] = "差评";
                $res['images'] = explode(',', $res['images']);
            } 
            if ($res) {
                ajax_return(1,'返回成功',$res);
            } else {
                ajax_return(0,'暂无评论',$res);
            }
        }
    }


    public function appEdit(){ 
        $post = Request::instance()->post(); 
        $app_id = $post['data']['app_id'];
        $is_display = $post['data']['is_display'];
        $result = Db::name('appraise')->where("app_id",$app_id)->update(["is_display"=>$is_display]);
        if($result){ 
            echo '1';
        }else{
            echo '修改失败';
        }
    }

}